export default {
  state: {
    // 需要缓存的组件
    keepAliveComponents: []
  },
  mutations: {
    // 对指定组件进行动态更改缓存（缓存）--组件调用该方法时，判断该组件是否存在于该缓存数组，无则添加
    isKeepAlive(state, component) {
      !state.keepAliveComponents.includes(component) && state.keepAliveComponents.push(component);
    },
    // 对指定组件进行动态更改缓存（不缓存）--组件调用该方法时，从缓存数组中删除对应的组件元素
    noKeepAlive(state, component) {
      const index = state.keepAliveComponents.indexOf(component);
      index > -1 && state.keepAliveComponents.splice(index, 1);
    }
  },
  actions: {
    // 提交需要缓存的组件
    commitIsKeepAlive(context, component) {
      context.commit('isKeepAlive', component);
    },
    // 提交需要缓存的组件
    commitNoKeepAlive(context, component) {
      context.commit('noKeepAlive', component);
    }
  }
};
